package cn.com.inossem.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 修改账号
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class AddAccountActivity extends RequestActivity {

	TextView addtext, huoqu, title;
	EditText phone, yanzhengma, mima, newmima, querennewmima;
	ImageView back;
	private MyCount mc;
	CheckBox checkbox;
	int type = 0;
	TextView xieyishu;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);
		type = getIntent().getIntExtra("type", 0);
		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		addtext = (TextView) this.findViewById(R.id.addtext);
		addtext.setOnClickListener(this);
		phone = (EditText) this.findViewById(R.id.phone);
		yanzhengma = (EditText) this.findViewById(R.id.yanzhengma);
		huoqu = (TextView) this.findViewById(R.id.huoqu);
		huoqu.setOnClickListener(this);
		mima = (EditText) this.findViewById(R.id.mima);
		newmima = (EditText) this.findViewById(R.id.newmima);
		querennewmima = (EditText) this.findViewById(R.id.querennewmima);
		xieyishu = (TextView) this.findViewById(R.id.xieyishu);
		xieyishu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Uri uri = Uri.parse("http://www.inossem.com/agreement.html");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});

		checkbox = (CheckBox) this.findViewById(R.id.checkbox);

		title = (TextView) this.findViewById(R.id.title);

		switch (type) {
		case 1:
			title.setText("修改密码");
			phone.setText(Utils.messageInfo.userInfo.getUsername());
			phone.setEnabled(false);
			newmima.setVisibility(View.VISIBLE);
			querennewmima.setVisibility(View.VISIBLE);

			LinearLayout checklayout = (LinearLayout) this
					.findViewById(R.id.checklayout);
			checklayout.setVisibility(View.GONE);

			RelativeLayout line2 = (RelativeLayout) this
					.findViewById(R.id.line2);
			LinearLayout line2xian = (LinearLayout) this
					.findViewById(R.id.line2xian);
			LinearLayout line3xian = (LinearLayout) this
					.findViewById(R.id.line3xian);
			LinearLayout line4xian = (LinearLayout) this
					.findViewById(R.id.line4xian);
			line2.setVisibility(View.GONE);
			line2xian.setVisibility(View.GONE);
			line3xian.setVisibility(View.VISIBLE);
			line4xian.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESSZHANGHAO)) {
			int errorFlag = bundle
					.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESSZHANGHAO);
			if (errorFlag == 1) {
				Toast.makeText(this, "修改成功!", Toast.LENGTH_LONG).show();
				Intent intent = new Intent();
				intent.putExtra("name", phone.getText().toString());
				setResult(99, intent);
				finish();
			} else if (errorFlag == 6) {
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "修改失败!", Toast.LENGTH_LONG).show();
			}

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			int errorFlag = bundle
					.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS);
			if (errorFlag == 1) {
				huoqu.setBackgroundResource(R.drawable.yanzhengmahui);
				mc = new MyCount(60000, 1000);
				mc.start();
			} else if (errorFlag == 4) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名已经存在！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 3) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名格式错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 5) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "验证码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 6) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 7) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "短信验证码获取错误！", Toast.LENGTH_LONG).show();
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_MIMASUCCESS)) {
			int errorFlag = bundle
					.getInt(WooRequestFactory.BUNDLE_EXTRA_MIMASUCCESS);
			if (errorFlag == 1) {
				Toast.makeText(this, "修改成功!", Toast.LENGTH_LONG).show();
				finish();
			} else if (errorFlag == 4) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名已经存在！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 3) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名格式错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 5) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "验证码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 6) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "用户名密码错误！", Toast.LENGTH_LONG).show();
			} else if (errorFlag == 7) {
				huoqu.setEnabled(true);
				Toast.makeText(this, "短信验证码获取错误！", Toast.LENGTH_LONG).show();
			}
		}

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.addaccount_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.huoqu:
			if (type == 0) {
				launchRequest(WooRequestFactory.Yanzhengma("phone", phone
						.getText().toString(), "2"));
				huoqu.setEnabled(false);
			}
			break;
		case R.id.addtext:
			if (type == 0) {
				if (!checkbox.isChecked()) {
					Toast.makeText(this, "请勾选同意用户使用协议", Toast.LENGTH_LONG)
							.show();
					return;
				}
				if (phone.getText().toString().length() > 0)
					launchRequest(WooRequestFactory.updatezhanghao(phone
							.getText().toString(), yanzhengma.getText()
							.toString(), mima.getText().toString(), ""));
			} else if (type == 1) {
				if (!newmima.getText().toString()
						.equals(querennewmima.getText().toString())) {
					Toast.makeText(this, "2次新密码不一致!", Toast.LENGTH_LONG).show();
					return;
				} else {
					launchRequest(WooRequestFactory.updatamina(mima.getText()
							.toString(), newmima.getText().toString()));

				}
			}
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK
		case 1:
			setResult(1);
			finish();
		default:
			break;
		}
	}

	/* 定义一个倒计时的内部类 */
	class MyCount extends CountDownTimer {
		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			huoqu.setBackgroundResource(R.drawable.yanzhengmahuang);
			huoqu.setTextColor(Color.WHITE);
			huoqu.setText("获取验证码");
			huoqu.setEnabled(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			huoqu.setText("短信已发送(" + millisUntilFinished / 1000 + "秒)");
			// Toast.makeText(RegisteredActivity.this,
			// millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//
			// toast有显示时间延迟
		}
	}
}
